package com.supreme.studyserver.controller;

import com.alibaba.fastjson2.JSONObject;
import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "remote")
public class RemoteController {

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "cache-server/call")
    public AjaxResp cacheServerCall() {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("trans-method", "cacheServerHello");
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(httpHeaders);
        ResponseEntity<JSONObject> exchange = restTemplate.exchange("http://localhost:7140/api/test/from/study-server", HttpMethod.GET, httpEntity, JSONObject.class);
        JSONObject resBody = exchange.getBody();
        return AjaxResp.instance(resBody);
    }
}
